package com.jjyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jjyu.dao.RestaurantMapper;
import com.jjyu.entity.Restaurant;
import com.jjyu.service.RestaurantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RestaurantServiceImpl implements RestaurantService {
    @Autowired
    private RestaurantMapper restaurantMapper;

    @Override
    public List<Restaurant> getAllRestaurant() {
        List<Restaurant> allRestaurant = restaurantMapper.getAllRestaurant();
        for (Restaurant restaurant : allRestaurant) {
            System.out.println(restaurant);
        }

        return allRestaurant;
    }

    @Override
    public List<Restaurant> getRestaurantByAddStr(String addStr) {
        QueryWrapper<Restaurant> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("address", addStr);
        List<Restaurant> restaurants = restaurantMapper.selectList(queryWrapper);
        for (Restaurant restaurant : restaurants) {
            System.out.println(restaurant);
        }
        return restaurants;
    }

    @Override
    public List<Restaurant> getRestaurantByPage(int pageNum, int pageSize) {
        System.out.println("pageNum: " + pageNum + ", pageSize: " + pageSize);
        IPage<Restaurant> page = new Page<>(pageNum, pageSize);
        IPage<Restaurant> result = restaurantMapper.selectPage(page, null);
        System.out.println("每页显示条数: " + result.getSize());
        System.out.println("Current page: " + result.getCurrent());
        System.out.println("Total pages: " + result.getPages());
        System.out.println("Records on current page: " + result.getRecords().size());
        List<Restaurant> restaurants = result.getRecords();
        for (Restaurant restaurant : restaurants) {
            System.out.println(restaurant);
        }
        return restaurants;
    }
}
